<template>
  <el-row type="flex" justify="space-between" align="middle" style="width:100%;height:40px;">
    <el-col>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <el-dropdown @command="operateDepts">
            <span>操作 <i class="el-icon-arrow-down" /></span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'

export default {
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: { // isRoot(是否根节点):是 则没有编辑删除  只在子节点显示
      type: Boolean,
      default: false
    }
  },
  methods: {
    operateDepts(type) { // 点击(添加编辑删除部门)时触发事件
      if (type === 'add') {
        this.$emit('addDepts', this.treeNode) // 告诉父组件显示弹层 // treeNode是点击添加的部门
      } else if (type === 'edit') {
        this.$emit('editDepts', this.treeNode)
      } else {
        this.$confirm('确定删除该部门吗').then(() => { // 点了确定进入then
          return delDepartments(this.treeNode.id) // 返回promise对象
        }).then(() => { // 成功了进入这里
          this.$emit('delDepts') // 触发自定义事件
          this.$message.success('删除部门成功')
        })
      }
    }
  }

}
</script>

<style>

</style>
